/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.mvc {
	import com.proyecto404.core.BaseObject;
	import com.proyecto404.core.util.FileHelper;
	
	import flash.display.DisplayObject;
	
	public class DefaultViewFactory extends BaseObject implements IViewFactory {
		private var _application:Application;
		public function DefaultViewFactory(application:Application) {
			super();
			_application = application;
		}
		
		public function createView(viewName:String, model:*=null):View {
			var viewInfo:ViewInfo = _application.configuration["views: " + viewName] as ViewInfo;
			var view:View = View(_application.library.getAssetFromSwf(viewInfo.className, FileHelper.getFileName(viewInfo.file)));
			view.initialize(_application, model);
			return view;
		}

		public function createLayout(viewName:String):LayoutView {
			var viewInfo:ViewInfo = _application.configuration["views: " + viewName] as ViewInfo;
			var view:LayoutView = LayoutView(_application.library.getAssetFromSwf(viewInfo.className, FileHelper.getFileName(viewInfo.file)));
			view.initialize(_application);
			return view;
		}
		
		public function containsView(viewName:String):Boolean {
			var viewInfo:ViewInfo = _application.configuration["views: " + viewName] as ViewInfo;
			var swfName:String = FileHelper.getFileName(viewInfo.file);
			if (!_application.library.containsSwf(swfName)) {
				return false;
			}
			var swf:DisplayObject = _application.library.getSwf(swfName);
			return swf.loaderInfo.applicationDomain.hasDefinition(viewInfo.className);

		} 
	}
}